import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
/*
 * Zone.java
 *
 * $LastChangedDate: 2008-05-29 11:00:51 +0000 (Thu, 29 May 2008) $
 * $LastChangedRevision: 38 $
 * Vicente J. Ferrer Dalmau
 * < vicente@jdalmau.es >
 *
 */

/**
 * This class represents each one of the squares to get painted in the Map.
 * @author Vicente J. Ferrer Dalmau
 */
public class Zone extends JPanel{
    
    Map MAP;
    public int ROW, COLUMN;
    public int NUMBER;
    public String imagePath;
    public boolean showId;
    public ArrayList availableColors = new ArrayList();
    public ArrayList constraints = new ArrayList();
    public MyColor color;
    // adjacent stuff
    public ArrayList adjacentZones = new ArrayList();
    // conflict set
    public ArrayList conflictSet = new ArrayList();
    
    /** Creates a new instance of Zone 
     * @param number -1 value means it's water and we don't have to colourize it.
     */
    public Zone(Map map, int row, int column, int number, String imagePath, boolean showId) {
        
        MAP = map;
        ROW = row;
        COLUMN = column;
        NUMBER = number;
        this.imagePath = imagePath;
        this.showId = showId;
        color = null;
        this.setBorder(new LineBorder(Color.black));
        this.setLayout(new FlowLayout());        
        
        // if its land show the number and if it has constraints or not in a ToolTip
        if (NUMBER != -1) {
            
            // init the available Colors, at the begining all the map colors.
            for (int i=0; i<MAP.COLORS; i++)
                availableColors.add(ColorsList.mycolors[i]);
            
            this.setToolTipText("Zona "+NUMBER);
            this.setBackground(ColorsList.earth.color);
            // label that will display "R" whether there's constraints  
            JLabel l = new JLabel();
            l.setText("");
            l.setForeground(Color.WHITE);
            add(l);
            
            // mouse click event handler
            addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jPanelClicked(evt);
                }
            });
        }
    }
    
    /** Override the paintComponent method to display a background image */
   public void paintComponent(Graphics g) {
        
       //System.out.println("number of colors: "+availableColors.size());
        if (NUMBER == -1) {
            // water
            Dimension size = getSize();
            ImageIcon back = new ImageIcon(getClass().getResource(imagePath));
            g.drawImage(back.getImage(), 0, 0, size.width, size.height, null);
            setOpaque(false);
        }
        else {
            // land
            if (color != null) {//.name != "tierra") {
                // it has already a color
                this.setBackground(color.color);
            }
            else if (constraints.size() != 0) {
                ((JLabel)this.getComponent(0)).setText("R");
            }
            else
                ((JLabel)this.getComponent(0)).setText("");
        }
        
        super.paintComponent(g);
    }
    
    /** Mouse click event code */
    private void jPanelClicked(java.awt.event.MouseEvent evt) {
        
        MAP.showConstraintsDialog(this);
    }
    
    /** Adds a new color's constraints array to the Zone.
     */
    public void addConstraints (Object[] colors) {
                
        availableColors = new ArrayList();
        constraints = new ArrayList();
        this.setToolTipText("Zona "+NUMBER);
        if (colors.length == 0 || colors.length == MAP.COLORS) {
            // no constraints or all the possible colors were selected
            for (int i=0; i<MAP.COLORS; i++)
                availableColors.add(ColorsList.mycolors[i]);
        }
        else {            
            this.setToolTipText(this.getToolTipText()+": ");
            for (int i=0; i<colors.length; i++) {
                MyColor c = (MyColor)colors[i];
                availableColors.add(c);
                constraints.add(c);
                this.setToolTipText(this.getToolTipText()+c.name+", ");
            }
        }
    }
    
    /** Gets the adjacent colors of this Zone
     */
    public ArrayList getAdjacentColors () {
        
        ArrayList a = new ArrayList();
        
        for (int i=0; i<this.adjacentZones.size(); i++) {
            if (((Zone)adjacentZones.get(i)).color != null)
                a.add(((Zone)adjacentZones.get(i)).color);
        }
        
        return a;
    }
    
    /** Clears the Zone in the map
     */
    public void clear () {
        
        this.color = null;
        this.availableColors = new ArrayList();
        this.conflictSet = new ArrayList();
        // the constraints will be kept        
        if (constraints.size() != 0) {
            for (int i=0; i<constraints.size(); i++)
                availableColors.add((MyColor)constraints.get(i));
        }
        else {
            for (int i=0; i<MAP.COLORS; i++)
                availableColors.add(ColorsList.mycolors[i]);
        }
        
        this.setBackground(ColorsList.earth.color);
    }
}
